#include "connectionpoint.h"

#include "graphicitemcommon.h"

#include <cmath>

using namespace gui;
using namespace swp;

ConnectionPoint::ConnectionPoint(
    qreal x, qreal y, int w, int h, QGraphicsItem *parent) :
        EllipseGraphicItem(QRectF(x, y, w, h), true, false, parent),
        m_gravitationRadius(PREFERRED_GRAVITATION_RADIUS)
{

}

bool ConnectionPoint::isInGravitationField(
    const QPointF& point, QPointF& center) const
{
    const QPointF pos = scenePos();
    const QRectF rect = boundingRect();
    const QPointF _center = center = QPointF(
        pos.x() + rect.width()/2, pos.y() + rect.height()/2);

    const qreal radius = std::max(rect.width(), rect.height())/2 +
        m_gravitationRadius;

    bool ret = pow(point.x() - _center.x(), 2) +
        pow(point.y() - _center.y(), 2) <= pow(radius, 2);

    return ret;
}

bool ConnectionPoint::gravitationFieldRadius()
    const
{
    return m_gravitationRadius;
}

void ConnectionPoint::setGravitationFieldRadius(
    qreal radius)
{
    m_gravitationRadius = radius;
}
